#include "process.h"
void process1()
{
    string lable("|/-\\");
    string nums;
    int count = 0;
    int lablesize = lable.size();
    nums.push_back(Head);
    while (count <= 100) 
    {
        cout << "[" << left << setw(100) << nums << "]";
        cout << "[" << "%" << count << "]";
        cout << "[" << lable[count%lablesize] << "]" << '\r';
        fflush(stdout);
        nums.clear();
        count++;
        nums.append(count, Body);
        if (count < 100)
        {
            nums.push_back(Head);
        }
        usleep(60000);
    }
    cout << endl;
}
//版本一的进度条只是无脑运行，不知道程序进度是多少，即没有依附应用进度，比如下载程序，进度条需依附于下载进度来跟进

//模拟一种场景，表示一种下载的任务
void download(callback_t cb)
{
    srand(time(0)*1024);
    int total = FileSize;
    while (total)
    {
        //下面表示一次下载动作
        usleep(10000);
        int one = rand() % (1024 * 1024 * 5);
        total -= one;
        if (total < 0) 
        {
            total = 0;
        }
        //表示当前的进度
        int download = FileSize - total;
        double rate = (download * 1.0 / (FileSize)) * 100.0;
        cb(rate);
    }
}
void process2(double rate)
{
    static string lable("|/-\\"); 
    static char buffer[Max] = { 0 };
    static int cnt = 0;
    if (rate <= 1.0)
    {
        buffer[0] = Head;
    }
    printf("\033[1;31;46m[%-100s]\033[0m[%.1lf%%][%c]\r", buffer, rate, lable[cnt % lable.size()]);
    fflush(stdout);
    buffer[(int)rate] = Body;
    if ((int)rate + 1 < 100)
    {
        buffer[(int)(rate + 1)] = Head;
    }
    if (rate >= 100.0)
    {
        cout << endl;
    }
    cnt++;
    cnt %= lable.size();
}
